﻿#include "Binay_Oper_Node.h"

std::string Binay_Oper_Node::generatePrefix() const
{

	bool left_prime = (type == Lexer::MUL || type == Lexer::DIV)
		&& (left->getType() == Lexer::PLUS || left->getType() == Lexer::MINUS);
	bool right_prime = (type == Lexer::MUL || type == Lexer::DIV)
		&& (right->getType() == Lexer::PLUS || right->getType() == Lexer::MINUS);
	std::string reStr;
	reStr += type;

	if (left_prime) reStr += "(";
	if (left != nullptr)
		reStr += left->generatePrefix();
	if (left_prime) reStr += ")";


	if (right_prime) reStr += "(";
	if (right != nullptr)
		reStr += right->generatePrefix();
	if (right_prime) reStr += ")";

	return reStr;

}

std::string Binay_Oper_Node::generateInfix() const
{
	bool left_prime = (type == Lexer::MUL || type == Lexer::DIV)
		&& (left->getType() == Lexer::PLUS || left->getType() == Lexer::MINUS);
	bool right_prime = (type == Lexer::MUL || type == Lexer::DIV)
		&& (right->getType() == Lexer::PLUS || right->getType() == Lexer::MINUS);
	std::string reStr;
	if (left_prime) reStr += "(";
	if (left != nullptr)
		reStr += this->getLeftChild()->generateInfix();
	if (left_prime) reStr += ")";

	reStr += type;

	if (right_prime) reStr += "(";
	if (right != nullptr)
		reStr += this->getRightChild()->generateInfix();
	if (right_prime) reStr += ")";

	return reStr;

}

std::string Binay_Oper_Node::generatePostfix() const
{
	bool left_prime = (type == Lexer::MUL || type == Lexer::DIV)
		&& (left->getType() == Lexer::PLUS || left->getType() == Lexer::MINUS);
	bool right_prime = (type == Lexer::MUL || type == Lexer::DIV)
		&& (right->getType() == Lexer::PLUS || right->getType() == Lexer::MINUS);
	std::string reStr;
	if (left_prime) reStr += "(";
	if (left != nullptr)
		reStr += left->generatePostfix();
	if (left_prime) reStr += ")";


	if (right_prime) reStr += "(";
	if (right != nullptr)
		reStr += right->generatePostfix();
	if (right_prime) reStr += ")";

	reStr += type;

	return reStr;

}
